package com.hyjx.framework.component.codetable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * 码表管理控制器
 */
@Controller
@RequestMapping("/codetable")
public class CodetableController {
    @Resource
    private CodetableService service;

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public ModelAndView gotoList(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/framework/codetable/list");
        mav.addObject("codetable", request.getParameter("codetable"));
        mav.addObject("codelevel", request.getParameter("codelevel"));
        return mav;
    }

    @RequestMapping(value = "/gotoListManager", method = RequestMethod.GET)
    public String gotoListManager(HttpServletRequest request) {
        return "/framework/codetable/listmanager";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList(CodetableModel model, HttpServletRequest request) {
        return service.queryList(model);
    }

    /**
     * 查询码表类别信息
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryManagerList", method = RequestMethod.POST)
    @ResponseBody
    public String queryManagerList(CodeTableManagerModel model, HttpServletRequest request) {
        if (model.getCodetable() != null && model.getCodetable().indexOf("_") > -1) {
            model.setCodetable(model.getCodetable().replaceAll("_", "\\\\_"));
        }
        return service.queryManagerList(model);
    }

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoAdd(HttpServletRequest request) {
        request.setAttribute("codetable", request.getParameter("codetable"));
        return "/framework/codetable/add";
    }

    /**
     * 跳转到类别的增加页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoManagerAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoManagerAdd(HttpServletRequest request) {
        return "/framework/codetable/addmanager";
    }

    /**
     * 增加信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(CodetableModel model) {
        try {
            if (service.add(model)) {
                return "success";
            } else {
                return "failure";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 跳转到修改页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");
        String codetable = request.getParameter("codetable");
        CodetableModel model = new CodetableModel();
        model.setCode(id);
        model.setCodetable(codetable);
        model = service.getInfoById(model);
        ModelAndView mav = new ModelAndView("/framework/codetable/edit");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 修改信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(CodetableModel model) {
        if (service.edit(model) > 0) {
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {

        String ids = request.getParameter("ids");
        String codetable = request.getParameter("codetable");
        CodetableModel model = new CodetableModel();
        model.setCode(ids);
        model.setCodetable(codetable);
        service.delete(model);
        return "success";
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        String codetable = request.getParameter("codetable");
        CodetableModel model = new CodetableModel();
        model.setCode(id);
        model.setCodetable(codetable);
        model = service.getInfoById(model);
        ModelAndView mav = new ModelAndView("/framework/codetable/view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 根据条件查询展现树的数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/codeSelect", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String codeSelect(HttpServletRequest request, HttpServletResponse response) {
        String codetable = request.getParameter("codetable");
        String parentcode = request.getParameter("parentcode");
        String showtype = request.getParameter("showtype");
        String pinyin = request.getParameter("pinyin");
        if (showtype == null) {
            return service.deal(codetable, parentcode, response, pinyin);
        } else {
            return service.dealComboTree(codetable, parentcode, response);
        }
    }
}